package main

import (
	"fmt"
	"sort"
)

func main() {
	var a map[string]string
	//使用map前,需要先make，为map分配空间
	a = make(map[string]string, 10)
	a["no1"] = "宋江"
	a["no2"] = "吴用"
	a["no3"] = "武松"
	b := make(map[string]string)
	b["no4"] = "aaa"
	hero := map[string]string{"hero1": "宋江", "hero2": "武松"}
	fmt.Println(a)
	fmt.Println(b)
	fmt.Println(hero)

	studentMap := make(map[string]map[string]string)
	studentMap["stu01"] = make(map[string]string, 2)
	studentMap["stu01"]["name"] = "tom"
	studentMap["stu01"]["sex"] = "男"
	studentMap["stu02"] = make(map[string]string)
	studentMap["stu02"]["name"] = "mary"
	studentMap["stu02"]["sex"] = "女"
	studentMap["stu02"]["address"] = "杭州"
	fmt.Println(studentMap)
	delete(studentMap["stu02"], "address")
	fmt.Println(studentMap)
	val, ok := studentMap["stu01"]["sex"]
	if ok {
		fmt.Printf("有值为%v", val)
	} else {
		fmt.Printf("没有")
	}

	cities := make(map[string]string)
	cities["no1"] = "北京"
	cities["no2"] = "上海"
	cities["no3"] = "杭州"
	for k, v := range cities {
		fmt.Printf("k=%v v=%v\n", k, v)
	}
	for k1, v1 := range studentMap {
		fmt.Println("k1=", k1)
		for k2, v2 := range v1 {
			fmt.Printf("k2=%v v2=%v", k2, v2)
		}
		fmt.Println()
	}
	fmt.Println("stu的数量=", len(studentMap))

	//map切片
	var mosters []map[string]string
	mosters = make([]map[string]string, 2)
	if mosters[0] == nil {
		mosters[0] = make(map[string]string, 2)
		mosters[0]["name"] = "牛魔王"
		mosters[0]["age"] = "500"
	}
	if mosters[1] == nil {
		mosters[1] = make(map[string]string, 2)
		mosters[1]["name"] = "玉兔精"
		mosters[1]["age"] = "400"
	}
	newMoster := map[string]string{
		"name": "火云邪神",
		"age":  "200",
	}
	mosters = append(mosters, newMoster)
	fmt.Println(mosters)

	map1 := make(map[int]int, 10)
	map1[1] = 13
	map1[2] = 5
	map1[10] = 100
	map1[4] = 56
	fmt.Println(map1)

	var keys []int
	for k, _ := range map1 {
		keys = append(keys, k)
	}
	sort.Ints(keys)
	fmt.Println(keys)
	for _, k := range keys {
		fmt.Printf("map[%v]=%v ", k, map1[k])
	}
}
